#ifndef __NORFLASH_H__
#define __NORFLASH_H__

#include <stdint.h>
#ifdef __cplusplus
 extern "C" {
#endif

#ifdef NORFLASH_VAR
#define NORFLASH_EXT 
#else
#define NORFLASH_EXT extern
#endif

#define SPI_FLASH_SIZE  	  	0x1000000
#define SPI_FLASH_PAGE_SIZE   	0x1000	//4K

#define FLASH_OP_BUF_SIZE  		0x1000


#define BUSY_MASK		0x01	// for MX25L3208
#define WREN			0x06
#define WRDI			0x04
#define RDID			0x9F
#define RDSR			0x05
#define WRSR			0x01
#define READ			0x03
#define FAST_READ		0x0B
#define SE4K			0x20
#define BE				0xD8	// block erase
#define CE				0xC7	// chip erase
#define PP				0x02	
#define CP				0xAD

#define SE64K			0xD8
#define SE256K			0xDC

#define _1KSIZE			0x400
#define _2KSIZE			0x800
#define _4KSIZE			0x1000
#define _32KSIZE		0x8000
#define _64KSIZE		0x10000

#define  SPI_MOSI_PIN   GPIO_PIN_7
#define  SPI_SCK_PIN    GPIO_PIN_5
#define  SPI_CS_PIN     GPIO_PIN_4
#define  SPI_MISO_PIN   GPIO_PIN_6




#define SCK_H		__GPIO_PIN_SET    (GPIOA, SPI_SCK_PIN) //hal_gpio_set_value(GPIO_GROUP(hal_gpio_name2pin(SPI_SCK_PIN)), GPIO_GROUP_PIN(hal_gpio_name2pin(SPI_SCK_PIN)),1)
#define SCK_L		__GPIO_PIN_RESET  (GPIOA, SPI_SCK_PIN) //hal_gpio_set_value(GPIO_GROUP(hal_gpio_name2pin(SPI_SCK_PIN)), GPIO_GROUP_PIN(hal_gpio_name2pin(SPI_SCK_PIN)),0)

#define MOSI_H		__GPIO_PIN_SET    (GPIOA, SPI_MOSI_PIN)//hal_gpio_set_value(GPIO_GROUP(hal_gpio_name2pin(SPI_MOSI_PIN)), GPIO_GROUP_PIN(hal_gpio_name2pin(SPI_MOSI_PIN)),1)
#define MOSI_L		__GPIO_PIN_RESET  (GPIOA, SPI_MOSI_PIN)//hal_gpio_set_value(GPIO_GROUP(hal_gpio_name2pin(SPI_MOSI_PIN)), GPIO_GROUP_PIN(hal_gpio_name2pin(SPI_MOSI_PIN)),0)

#define MISO_H      __GPIO_PIN_SET    (GPIOA, SPI_MISO_PIN)//hal_gpio_set_value(GPIO_GROUP(hal_gpio_name2pin(SPI_MISO_PIN)), GPIO_GROUP_PIN(hal_gpio_name2pin(SPI_MISO_PIN)),1)
#define MISO_L      __GPIO_PIN_RESET  (GPIOA, SPI_MISO_PIN)//hal_gpio_set_value(GPIO_GROUP(hal_gpio_name2pin(SPI_MISO_PIN)), GPIO_GROUP_PIN(hal_gpio_name2pin(SPI_MISO_PIN)),0)

#define ReadMISO()  __GPIO_INPUT_PIN_GET(GPIOA, SPI_MISO_PIN)

NORFLASH_EXT uint32_t op_timeout;

NORFLASH_EXT uint8_t sd;

NORFLASH_EXT uint8_t flash_idbug[5];

#define FLASH_SEL()		__GPIO_PIN_RESET(GPIOA, SPI_CS_PIN)//hal_gpio_set_value(GPIO_GROUP(hal_gpio_name2pin(SPI_CS_PIN)), GPIO_GROUP_PIN(hal_gpio_name2pin(SPI_CS_PIN)),0)
#define FLASH_DESEL()	__GPIO_PIN_SET  (GPIOA, SPI_CS_PIN)//hal_gpio_set_value(GPIO_GROUP(hal_gpio_name2pin(SPI_CS_PIN)), GPIO_GROUP_PIN(hal_gpio_name2pin(SPI_CS_PIN)),1)
	 
	 
NORFLASH_EXT uint8_t SPI1_TX_Buff ;
volatile NORFLASH_EXT uint8_t ReadStatus, fReadOnlyEnd,WriteStatus;

NORFLASH_EXT void SPI_gpio_init(void);
NORFLASH_EXT unsigned char SpiFlash_ReadID(void);




void send_data_spi(unsigned char dat);
unsigned char read_data_spi(void);

void FlashWriteEn(void);
void FlashWriteDis(void);
void FlashWriteUnlock(void);
//unsigned char FlashRDSR(void);
uint32_t FlashRDSR(void);
void FlashSE(unsigned char erase_command,unsigned int Addr);
void FlashWrite(unsigned int Addr,unsigned char* sdat,int length);
void FlashRead(unsigned int Addr,unsigned char* rdat,int length);

void DMAx_Init(void);
void SPI_W25Q128_ReadBytes(unsigned char *DataBuffer, unsigned long Len);
uint8_t SPI_W25Q128_WriteBytes(unsigned char DataBuffer );
void SPI_Flash_Read1(unsigned char* pBuffer,unsigned long ReadAddr,unsigned int NumByteToRead)  ;
uint8_t SPI1_Transceiver(uint8_t dat);
void SPI_Flash_Erase_Chip(void) ;
 
 
void show_number(uint8_t x,uint8_t y,uint8_t number,uint16_t color);

#endif
